package com.itcast.cn.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;

/**
 * <p>request对象的学习</p>
 *
 * 通用方式获取参数
 *      Map<String,String[]> getParameterMap()：获取所有参数Map集合
 *      String[] getParameterValues(String name)：根据名称获取参数值（数组）
 *      String getParameter(String name)：根据名称获取参数值(单个值)
 *
 * @author hyatt 2022/1/17 17:44
 * @version 1.0
 */
@WebServlet("/request2")
public class RequestTest2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // Map<String,String[]> getParameterMap()：获取所有参数Map集合
        Map<String, String[]> parameterMap = req.getParameterMap();
        for (String key : parameterMap.keySet()) {
            // 获取键对应的值并输出
            for (String value : parameterMap.get(key)) {
                System.out.println(key + ":" + value + " ");
            }
            // 每一行输出完并换行
            System.out.println("");
        }
        System.out.println("===============================");
        // String[] getParameterValues(String name)：根据名称获取参数值（数组）
        String[] hobbies = req.getParameterValues("hobby");
        for (String hobby : hobbies) {
            System.out.println(hobby);
        }
        System.out.println("===============================");
        // String getParameter(String name)：根据名称获取参数值(单个值)
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username);
        System.out.println(password);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
